<?php

namespace api\controllers;

use Yii;
use common\models\Information;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * InformationController implements the CRUD actions for Information model.
 */
class InformationController extends Controller
{

    public function actionApi(){
        $agent = Information::findBySql('SELECT * FROM information')->all();
        $ret=[];
        foreach($agent as $v){
            $re['asd'][]= [
                'id' => $v->id,
                'cover_img' => $v->cover_img,
                'content' => $v->content,
                'title' => $v->title,
                'link' => $v->link,
                'commentcount' => $v->commentcount,
                'browsecount' => $v->browsecount,
                'created_at' => $v->created_at,
            ];
        }
        return json_encode($re);
    }

    /**
     * Lists all Information models.
     * @return mixed
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => Information::find(),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Information model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Information model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Information();
        if (Yii::$app->request->isPost) {
            $model->load(Yii::$app->request->post());
            $img = Yii::$app->imgload->UploadPhoto($model,'/uploads/coverimg/','cover_img');
            $model->cover_img = $img;//存入表中
            $model->top = 1;
            $model->type = 1;
            $model->commentcount = 5000;
            $model->browsecount = 5000;
            $model->status = 1;
            $model->created_at = time();
            $model->updated_at = time();
            if (!$model->save()) {
                echo '<pre>';print_r($model->getErrors());die;
            }
        }
        if ($model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Information model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Information model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Information model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Information the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Information::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
